<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-HTTP.
 *
 * PHP-HTTP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-HTTP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-HTTP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-http
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

final class Locale {


    // ----------------------------------------------------- Instance Variables


    /**
     * The language code, as returned by getLanguage().
     *
     * @serial the languange, possibly ""
     */
    private $language;


    /**
     * The country code, as returned by getCountry().
     *
     * @serial the country, possibly ""
     */
    private $country;


    /**
     * The default locale. Except for during bootstrapping, this should never be
     * null. Note the logic in the main constructor, to detect when
     * bootstrapping has completed.
     */
    private static $defaultLocale;


    // ----------------------------------------------------------- Constructors


    /**
     * Creates a new locale for the given language and country.
     *
     * @param language lowercase two-letter ISO-639 A2 language code
     * @param country uppercase two-letter ISO-3166 A2 contry code
     * @param variant vendor and browser specific
     * @throws NullPointerException if any argument is null
     */
    public function __construct($language, $country = "", $variant = "") {
        $this->language = strtolower($language);
        $this->country  = strtoupper($country);
        $this->variant  = $variant;
    }


    // --------------------------------------------------------- Public Methods
    

    /**
     * Returns the default Locale. The default locale is generally once set
     * on start up and then never changed. Normally you should use this locale
     * for everywhere you need a locale. The initial setting matches the
     * default locale, the user has chosen.
     *
     * @return the default locale for this virtual machine
     */
    public static function getDefault() {
        return new Locale("en");
    }


    /**
     * Returns the language code of this locale. Some language codes have changed
     * as ISO 639 has evolved; this returns the old name, even if you built
     * the locale with the new one.
     *
     * @return language code portion of this locale, or an empty String
     */
    public function getLanguage()
    {
        return $this->language;
    }


    /**
    * Returns the country code of this locale.
    *
    * @return country code portion of this locale, or an empty String
    */
    public function getCountry()
    {
        return $this->country;
    }


    /**
    * Returns the variant code of this locale.
    *
    * @return the variant code portion of this locale, or an empty String
    */
    public function getVariant()
    {
        return $this->variant;
    }


    /**
     * Gets the string representation of the current locale. This consists of
     * the language, the country, and the variant, separated by an underscore.
     * The variant is listed only if there is a language or country. Examples:
     * "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC".
     *
     * @return the string representation of this Locale
     * @see #getDisplayName()
     */
    public function __toString()
    {
        if (strlen($this->language) == 0 && strlen($this->country) == 0)
            return "";
        else if (strlen($this->country) == 0 && strlen($this->variant) == 0)
            return $this->language;

        $result = $this->language;
        $result .= '_'.$this->country;
        if (strlen($this->variant) != 0)
            $result .= '_'.$this->variant;
            
        return $result;
    }
    
}
?>